import Vue from "vue"
import Router from "vue-router"
Vue.use(Router)

import loginRoute from "./constant"
import constantLayoutRoutes from "./constant-layout"

export const routes = constantLayoutRoutes

//自动引入模块路由，路由为模块下index.js文件
const dynamicRoute = window.configObj.dynamicRoute || process.env.VUE_APP_DYNAMIC_ROUTE
export let constantRoute = []
if (dynamicRoute === "1") { //动态添加路由
  constantRoute = loginRoute
} else {
  const routerContext = require.context("./", true, /index\.js$/)
  routerContext.keys().forEach(route => {
    if (route.startsWith("./index") || route.startsWith("./constant")) {
      return
    }
    const routerModule = routerContext(route)

    constantRoute = [...constantRoute, ...(routerModule.default || routerModule)]
  })
}

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  linkActiveClass: "active",
  routes: constantRoute
})
const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
export default router
